package com.car.controller;

import com.car.entity.Coupon;
import com.car.entity.Exchangetable;
import com.car.entity.GrabThe;
import com.car.entity.Integral;
import com.car.entity.User;
import com.car.service.ExchangetableService;
import com.car.service.GrabTheService;
import com.car.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/grabe")
public class GrabeTheController {
	@Autowired
	GrabTheService grabTheService;
	@Autowired
	UserService userService;
	@Autowired
	ExchangetableService exchangetableService;
	
	Map<String, Object> maps = new HashMap<String, Object>();
	
	/**
	 * 积分
	    * @Title: intrgral
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping({"/integrals",""})
	public String intrgral( Model model,HttpSession session){
		List<GrabThe> list = grabTheService.selectAll();
		User user = (User)session.getAttribute("loginSuccess");
		User users = userService.selectByPrimaryKey(user.getUserId());
		model.addAttribute("grabList", list);
		model.addAttribute("user", users);
		return "H5phone/integral";
	}
	
	@RequestMapping({"/shopinfos",""})
	public String shopUtils(@RequestParam("grabId")Integer grabId,
			@RequestParam(value="pan",required=false)String pan, HttpSession session, Model model){
		User user = (User)session.getAttribute("loginSuccess");
		
		GrabThe grabe = grabTheService.selectAllBygrabId(grabId);
		User users = userService.selectByPrimaryKey(user.getUserId());
		model.addAttribute("pan", pan!=null?1:null);
		model.addAttribute("users",users);
		model.addAttribute("grabe", grabe);
		return "H5phone/shopping";
	}
	
	@RequestMapping("/shoppingUpd")
	@ResponseBody
	public Map<String, Object>shopUpd(Integer grabId,HttpSession session){
		User user = (User)session.getAttribute("loginSuccess");
		User users = userService.selectByPrimaryKey(user.getUserId());
		GrabThe grabe = grabTheService.selectAllBygrabId(grabId);
		if(users.getVipScore()<grabe.getTheIntegral()){
			
			return maps;
		}
		Exchangetable exchan = new Exchangetable();
		exchan.setUserId(user.getUserId());
		exchan.setIntegralNumber(grabe.getTheIntegral());
		exchan.setGrabId(grabId);
		Coupon coupon = new Coupon();
		Integral inte = new Integral();
		 int nums= exchangetableService.insert(exchan,grabe.getTheIntegral(),coupon,grabe,inte);
		 maps.put("msg", nums>0?true:false);
		return maps;
	}
	

	
  
}